package com.sfac.javaSpringBoot.modules.account.controller;

import com.sfac.javaSpringBoot.modules.account.entity.User;
import com.sfac.javaSpringBoot.modules.account.service.UserService;
import com.sfac.javaSpringBoot.modules.common.entity.Result;
import com.sfac.javaSpringBoot.modules.common.entity.SearchBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class UserController {
    private UserService userService;
    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    /**
     * 127.0.0.1/api/login ---- post
     * {"userName":"admin", "password":"111111"}
     */
    @PostMapping(value = "/login", consumes = "application/json")
    public Result<User> login(@RequestBody User user) {
        return userService.login(user);
    }


    //127.0.0.1/user/insert
//    {"username":"李四","email":"102@qq.com","age":"10"}
    @PostMapping(value = "/user",consumes = "application/json")
    public Result<User> insertUser(@RequestBody User user){
        return userService.insertUser(user);
    }




//    127.0.0.1/user/update
    @PutMapping(value = "/user",consumes = "application/json")
    public Result<User> updateUser(@RequestBody User user) {
        return userService.updateUser(user);
    }
    //    127.0.0.1/user/delete/2
    @DeleteMapping(value = "/user/{id}")
    public Result<User> deleteUserById(@PathVariable int id){
        return userService.deleteUserById(id);
    }


//    jpa
//    127.0.0.1/user/getuser?name=lisi&email=102@qq.com
//    @GetMapping("/user")
//    public User findFirstByNameAndEmail(
//                                        @RequestParam    String name,
//                                        @RequestParam    String email){
//        return userService.findFirstByUserNameAndEmail(name,email);
//    }
//    127.0.0.1/user/insertUsers
//    [{"name":"aa","email":"aa@163.com","password":18},{"name":"aa1","email":"aa1@163.com","password":18}]

//    @PostMapping(value = "/insertUsers")
//    public Result<Object> insertUsers(@RequestBody List<User> users){
//        return userService.insertUsers(users);
//    }
    /**
     * 127.0.0.1/user/getUser/3 ---- get
     */

    @GetMapping("/user/{id}")
    public User getUserById(@PathVariable int id){
        return userService.getUserById(id);
    }



//    127.0.0.1/user/getAllUser
//    @GetMapping("/getAllUser")
//    public List<User> getAllUser(){
//        return userService.getAllUser();
//    }
//    //127.0.0.1/user/deleteUser/3
//    @DeleteMapping("/deleteUser/{id}")
//    public Result<Object> deleteUserByIdJpa(@PathVariable int id){
//        return userService.deleteUserByIdJpa(id);
//    }
////    更新数据
//    //127.0.0.1/user/editUser
////{"id":4,"userName":"hujiangggg","email":"hjjjj@163.com","password":18}
//    @PutMapping(value = "/editUser",consumes = "application/json")
//    public Result<User> editUser(@RequestBody User user){
//        return userService.editUser(user);
//    }
//    分页查询
//    127.0.0.1/user/users
//    {"currentPage":1,"pageSize":5,"orderBy":"userName","direction":"desc","keyword":""}
    @PostMapping(value = "/users",consumes = "application/json")
    public Page<User> getUsersBySearchBean(@RequestBody SearchBean searchBean){
        return userService.getUsersBySearchBean(searchBean);
    }
//    127.0.0.1/user/getUserById/{id}
    @GetMapping(value = "/getUserById/{id}")
    public User getUserByIdForSql(@PathVariable int id){
        return userService.getUserByIdForSql(id);
    }
}

